package com.ruoyi.contract.controller;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.contract.domain.InvoiceMain;
import com.ruoyi.contract.service.IInvoiceMainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发票管理Controller
 * 
 * @author ruoyi
 * @date 2021-04-25
 */
@RestController
@RequestMapping("/contract/invoice")
public class InvoiceMainController extends BaseController
{
    @Autowired
    private IInvoiceMainService invoiceMainService;

    /**
     * 查询发票管理列表
     */
    @PreAuthorize("@ss.hasPermi('contract:invoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(InvoiceMain invoiceMain)
    {
        startPage();
        List<InvoiceMain> list = invoiceMainService.selectInvoiceMainList(invoiceMain);
        return getDataTable(list);
    }

    /**
     * 导出发票管理列表
     */
    @PreAuthorize("@ss.hasPermi('contract:invoice:export')")
    @Log(title = "发票管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InvoiceMain invoiceMain)
    {
        List<InvoiceMain> list = invoiceMainService.selectInvoiceMainList(invoiceMain);
        ExcelUtil<InvoiceMain> util = new ExcelUtil<InvoiceMain>(InvoiceMain.class);
        return util.exportExcel(list, "发票管理数据");
    }

    /**
     * 获取发票管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('contract:invoice:query')")
    @GetMapping(value = "/{gid}")
    public AjaxResult getInfo(@PathVariable("gid") String gid)
    {
        return AjaxResult.success(invoiceMainService.selectInvoiceMainById(gid));
    }

    /**
     * 新增发票管理
     */
    @PreAuthorize("@ss.hasPermi('contract:invoice:add')")
    @Log(title = "发票管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InvoiceMain invoiceMain)
    {
        invoiceMain.setTaxAmount(invoiceMain.getInvoiceAmount().multiply(invoiceMain.getTaxRate()).multiply(new BigDecimal("0.01")));
        return toAjax(invoiceMainService.insertInvoiceMain(invoiceMain));
    }

    /**
     * 修改发票管理
     */
    @PreAuthorize("@ss.hasPermi('contract:invoice:edit')")
    @Log(title = "发票管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InvoiceMain invoiceMain)
    {
        invoiceMain.setTaxAmount(invoiceMain.getInvoiceAmount().multiply(invoiceMain.getTaxRate()).multiply(new BigDecimal("0.01")));
        return toAjax(invoiceMainService.updateInvoiceMain(invoiceMain));
    }

    /**
     * 删除发票管理
     */
    @PreAuthorize("@ss.hasPermi('contract:invoice:remove')")
    @Log(title = "发票管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gids}")
    public AjaxResult remove(@PathVariable String[] gids)
    {
        return toAjax(invoiceMainService.deleteInvoiceMainByIds(gids));
    }

    /**
     * 确认发票管理
     */
    @PreAuthorize("@ss.hasPermi('contract:invoice:approve')")
    @GetMapping(value = "/approve/{gid}")
    public AjaxResult approve(@PathVariable String gid)
    {
        return toAjax(invoiceMainService.approveInvoiceMainById(gid));
    }
}
